#include "../head/formtext.h"
#include "ui_formtext.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextCodec>
#include <QProcess>
#include <QDebug>
#include <QCoreApplication>
#include "../head/utils/MutiSignalDecode.h"
#include "../head/utils/MutiSerialControl.h"
extern MutiSignalDecode *mutiSignalDecode;
extern MutiSerialControl *mutiSerialControl;

//-----------------------125Hz----------------------------------
#define DL_DAT_PKT_TYP_CH_PSK_25x5Hz4order    0x80
#define DL_DAT_PKT_TYP_CH_PSK_25x5Hz2order    0x81
#define DL_DAT_PKT_TYP_CH_PSK_25x5Hz1order    0x82
//-----------------------25Hz----------------------------------
#define DL_DAT_PKT_TYP_CH_PSK_25x1Hz4order    0x83
#define DL_DAT_PKT_TYP_CH_PSK_25x1Hz2order    0x84
#define DL_DAT_PKT_TYP_CH_PSK_25x1Hz1order    0x85
#define DL_DAT_PKT_TYP_CH_PSK_12p5x2Hz4order    0x86
#define DL_DAT_PKT_TYP_CH_PSK_12p5x2Hz2order    0x87
#define DL_DAT_PKT_TYP_CH_PSK_12p5x2Hz1order    0x88
#define DL_DAT_PKT_TYP_CH_PSK_6p25x4Hz4order    0x89
#define DL_DAT_PKT_TYP_CH_PSK_6p25x4Hz2order    0x8A
#define DL_DAT_PKT_TYP_CH_PSK_6p25x4Hz1order    0x8B
#define DL_DAT_PKT_TYP_CH_PSK_5x5Hz4order      0x8C
#define DL_DAT_PKT_TYP_CH_PSK_5x5Hz2order      0x8D
#define DL_DAT_PKT_TYP_CH_PSK_5x5Hz1order      0x8E
//-----------------------18.75Hz--------------------------------
#define DL_DAT_PKT_TYP_CH_PSK_6p25x3Hz4order    0x8F
#define DL_DAT_PKT_TYP_CH_PSK_6p25x3Hz2order    0x90
#define DL_DAT_PKT_TYP_CH_PSK_6p25x3Hz1order    0x91
//-----------------------12.5Hz--------------------------------
#define DL_DAT_PKT_TYP_CH_PSK_6p25x2Hz4order    0x92
#define DL_DAT_PKT_TYP_CH_PSK_6p25x2Hz2order    0x93
#define DL_DAT_PKT_TYP_CH_PSK_6p25x2Hz1order    0x94
//-----------------------10Hz----------------------------------
#define DL_DAT_PKT_TYP_CH_PSK_5x2Hz4order       0x95
#define DL_DAT_PKT_TYP_CH_PSK_5x2Hz2order       0x96
#define DL_DAT_PKT_TYP_CH_PSK_5x2Hz1order        0x97

#define DL_DAT_PKT_TYP_CH_DBPSK_6p25x4Hz1order    0x98
#define DL_DAT_PKT_TYP_CH_PSK_PSK                 0x99


FormText::FormText(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormText)
{
    ui->setupUi(this);
    this->setMaximumSize(this->size());
    this->setMinimumSize(this->size());
    //ui->Fre_i->addItems({"5","12.5","25","6.25"});
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_25x5Hz4order-0x80");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_25x5Hz2order-0x81");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_25x5Hz1order-0x82");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_25x1Hz4order-0x83");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_25x1Hz2order-0x84");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_25x1Hz1order-0x85");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_12.5x2Hz4order-0x86");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_12.5x2Hz2order-0x87");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_12.5x2Hz1order-0x88");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x4Hz4order-0x89");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x4Hz2order-0x8A");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x4Hz1order-0x8B");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_5x5Hz4order-0x8C");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_5x5Hz2order-0x8D");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_5x5Hz1order-0x8E");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x3Hz4order-0x8F");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x3Hz2order-0x90");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x3Hz1order-0x91");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x2Hz4order-0x92");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x2Hz2order-0x93");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_6.25x2Hz1order-0x94");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_5x2Hz4order-0x95 ");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_5x2Hz2order-0x96 ");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_5x2Hz1order-0x97");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_DBPSK_6.25x4Hz1order-0x98");
    ui->SysParams->addItem("DL_DAT_PKT_TYP_CH_PSK_PSK-0x99");
    on_SysParams_activated(NULL);
}

FormText::~FormText()
{
    delete ui;
}


void FormText::modifyByRegExp(QString fileSource,QString fileDest)
{
    //打开ini文件
    QFile fileRead(QString::fromLocal8Bit(code->fromUnicode(fileSource)));       //先读取文件信息，QIODevice::Text能将换行转为\n
    QString arryRead;
    if(fileRead.open(QIODevice::ReadOnly|QIODevice::Text)){
        arryRead= fileRead.readAll();
    }
    fileRead.close();



   QStringList arryListWrite= arryRead.split("\n");        //存储每一段信息

   QString str_union = ui->Fre_i->currentText()+"x"+ ui->Channel_i->currentText()+"Hz";
   QString str_template_url = baseUrl+"/template/UpLinkReceiver_CenterFre_"+str_union+".txt";
   //打开滤波器
   QFile fileReadTemplate(QString::fromLocal8Bit(code->fromUnicode(str_template_url)));       //先读取文件信息，QIODevice::Text能将换行转为\n
   QString strReadTemplate;
   if(fileReadTemplate.open(QIODevice::ReadOnly|QIODevice::Text)){
       strReadTemplate= fileReadTemplate.readAll();
   }else{
        QMessageBox::information(this,"提示","滤波器文件不存在!");
   }
   fileReadTemplate.close();
   QString sysParams = ui->SysParams->currentText();
   //另存为
    QFile fileWrite(QString::fromLocal8Bit(code->fromUnicode(fileDest)));
    if(fileWrite.open(QIODevice::WriteOnly|QIODevice::Text)){        //文件流写文件
        QTextStream streamWrite(&fileWrite);      //通过文本流控制文件写操作
        int size = arryListWrite.size();

        for(int i=0;i<size;i++){
            QString contentWrite= arryListWrite.at(i);
            if(contentWrite.contains("Fre$")){
                contentWrite.replace("Fre$",QString::number(ui->Fre_i->currentIndex()+1));
            }else if(contentWrite.contains("Channel$")){
                contentWrite.replace("Channel$",ui->Channel_i->currentText());
            }else if(contentWrite.contains("Mod$")){
                contentWrite.replace("Mod$",ui->Mod_Order->currentText());
            }else if(contentWrite.contains("$Demodule")){
                bool ok;
                contentWrite.replace("$Demodule",QString::number(sysParams.right(2).toInt(&ok, 16)));
            }else if(contentWrite.contains("$PreSignal")){
                contentWrite.replace("$PreSignal",ui->PreSignal_Length->currentText());
            }else if(contentWrite.contains("Mseq$")){
                //(6)PSK_PSK用的调制模式为：5x5Hz1order和5x2Hz1order 用的M序列为前16位和后16位
                if(ui->SysParams->currentText()=="DL_DAT_PKT_TYP_CH_PSK_PSK-0x99"){
                    if(ui->Channel_i->currentText() == "2")contentWrite.replace("Mseq$","\"0001111100110100\"");
                    else contentWrite.replace("Mseq$","\"1000010101110110\"");
                }else{
                    contentWrite.replace("Mseq$","\"1000010101110110\"");
                }
            }else if(contentWrite.contains("S1$")){
                contentWrite.replace("S1$",strReadTemplate);
            }else if(contentWrite.contains("FracEqRatio$")){
                if(ui->SysParams->currentText()=="DL_DAT_PKT_TYP_CH_DBPSK_6.25x4Hz1order-0x98"){
                    contentWrite.replace("FracEqRatio$","32");
                }else{
                    contentWrite.replace("FracEqRatio$","10");
                }
            }

            streamWrite<<contentWrite<<"\n";
        }        
        fileWrite.close();
        QMessageBox::StandardButton sButton = QMessageBox::question(NULL, (QObject::tr("提示")), (QObject::tr("更改配置文件需要重启软件，需要立即重启吗？")));
        if (sButton == QMessageBox::StandardButton::Yes)
        {
            if(mutiSignalDecode != NULL){
                mutiSerialControl->stop_Serial(0);
                mutiSerialControl->stop_Serial(1);
                mutiSignalDecode->free_Param();
            }

            QCoreApplication::exit(0);
        }

    }else{
        fileWrite.close();
        QMessageBox::information(this,"提示","文件另存失败");
    }

}




void FormText::on_toolButton_clicked()
{
    QString savefilename = QFileDialog::getSaveFileName(
                    this,                //父组件
                    "save file",         //标题
                    "./",                //起始路径
                    "ini(*.ini)");      //过滤器

    QString str_conf_url = "";
    if(savefilename.toUpper().contains("LOW")){
         str_conf_url = baseUrl+"/template/InitParam_Low.ini";
    }else{
        str_conf_url = baseUrl+"/template/InitParam_High.ini";

    }




    modifyByRegExp(str_conf_url,savefilename);

//    if(mutiSignalDecode != NULL){
//        mutiSerialControl->stop_Serial(0);
//        mutiSerialControl->stop_Serial(1);
//        mutiSignalDecode->free_Param();
//    }

//    mutiSignalDecode = new MutiSignalDecode();
//    mutiSerialControl->start_Serial(0);
//    mutiSerialControl->start_Serial(1);
}


void FormText::on_SysParams_activated(const QString &arg1)
{
    QString str = ui->SysParams->currentText();
   // QRegExp regExp("\\d+"); // 匹配一个或多个数字
    QRegExp regExp("[0-9]+\\.[0-9]+|[0-9]+"); // 匹配一个或多个数字

   // QRegularExpression regExp2("\\d+\\.\\d+");

    int pos = 0;
    QString params[6];
    int i = 0;
    while ((pos = regExp.indexIn(str, pos)) != -1) {
        QString number = regExp.cap(0); // 提取匹配到的第一个数字
        qDebug() << "Number: " << number;
        pos += regExp.matchedLength(); // 更新搜索的起始位置
        params[i++] = number;
    }

    if(params[0] != NULL && params[1] != NULL && params[2] !=NULL){
        ui->Fre_i->setCurrentText(params[0]);
        ui->Channel_i->setCurrentText(params[1]);
        ui->Mod_Order->setCurrentText(params[2]);
    }else{ //PSK_PSK特殊处理
        ui->Fre_i->setCurrentText("5");
        ui->Channel_i->setCurrentText("2");
        ui->Mod_Order->setCurrentText("1");
    }

    if(str.right(2) == "98"){ //DBPSK
        ui->PreSignal_Length->setCurrentText("1280");
    }else if(params[0].contains(".")){ //包括小数
        ui->PreSignal_Length->setCurrentText("1024");
    }else{
        ui->PreSignal_Length->setCurrentText("1000");

    }

}



